<?php
namespace App\Http\Controllers\Service;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Weixin\WeixinServiceController;
use App\Models\V1\NewOrderTemplateModel;
use App\Models\V1\OrderProcessTemplateModel;
use Illuminate\Http\Request;
use App\Common\MobileMassege;
use App\Common\ApiData;
use DB;
USE App\Models\V1\MemberModel;

class Mobile extends Controller{

    public function get_code(Request $request){
        $mobile = $request->input('mobile');
        $country= $request->input('country',86);
        $msgModel   = new MobileMassege();
        $apiData    = new ApiData();
        $msgModel->countryCode  = $country;
        $msgModel->mobile       = $mobile;
        $result = $msgModel->send_valid_code();
        if($result){
            $apiData->message   = "验证码发送成功";
            $apiData->code      = 200;
        }else{
            $apiData->message   = "发送失败";
            $apiData->code      = 200;
        }
        return $apiData->send();
    }


    public function check_code(Request $request){

        $mobile = $request->input('mobile');
        $code   = $request->input('code');

        $msgModel   = new MobileMassege();
        $result     = $msgModel->check_valid_code($mobile,$code);
        $apiData    = new ApiData();
        $apiData->message   = "ok";
        $apiData->code      = 200;

        if($result === true){
            $apiData->set_data('valid',1);
        }else{
            $apiData->set_data('valid',0);
        }
        return $apiData->send();
    }


    public function send_mobile_code_callback(Request $request)
    {
        $info   = $request->all();
        $msgId  = $info['msgid'];
        if(strtolower($info['status']) != "delivrd") //发送失败
        {
            $memberModel        = new MemberModel();
            $memberinfo         = $memberModel->get_member_info_by_mobile($info['mobile']);
            $userName           = $memberinfo ? $memberinfo->YourName : '新会员';
            $status             = $info['statusDesc'];
            $msg                = $userName.':'.$info['mobile']."失败,原因:".$status.",msgId:".$msgId;
            $data               = new OrderProcessTemplateModel();
            $data->openId       = "oPF8hxG8_2YImTxcJlfrq-jTGCuI";
            $data->orderInfo    = '客户短信发送失败';
            $data->orderSn      = "111";
            $data->orderStatus  = "222";
            $data->otherInfo    = $msg;
            $data->orderTime    = date_time();
            $data->orderType    = "登录平台：创蓝，账户：15000606942密码：xiaonangua";
            $data->url          = "https://zz.253.com";
            $weixinService      = new WeixinServiceController();
            $weixinService->send_order_process_template($data);
        }
        DB::table('mobilecode')->where('msg_id',$msgId)->update(['msg'=>json_encode($info)]);
        return $info;
    }

}